package com.yc.register.dao;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@ConfigurationProperties(prefix = "myconfig.reg")
@Repository
public class RegisterDao {
    //注入环境参数

    private String userTable;
    private String countColumn;
    private String nameColumn;
    private String pwdColumn;
    private String genderColumn;
    private String ageColumn;
    private String addressColumn;
    private String phoneColumn;
    private String emailColumn;
    private String schoolColumn;
    private String educationColumn;
    private String professionColumn;
    private String jobColumn;
    private String pcodeColumn;
    private String pwdColumn1;


    @Resource
    private JdbcTemplate jdbcTemplate;


    public int register(String account, String name, String pwd, String address, String phone, String email){
        String sql = "insert into %s set %s=?, %s=?, %s=?, %s=?, %s=?, %s=?";
        sql = String.format(sql,userTable,countColumn,nameColumn,pwdColumn,addressColumn,phoneColumn,emailColumn);
        try {
            return jdbcTemplate.update(sql,account,name,pwd,address,phone,email);
        }catch (IncorrectResultSizeDataAccessException e){
            return 0;
        }
    }

    public Object editPerson(String name, String gender, String age, String address,
                             String phone, String email, String school, String education, String profession,
                             String job, String pcode,String account, String pwd){
        String sql = "update %s set %s=?, %s=?, %s=?, %s=?, %s=?, %s=?, %s=?, %s=?, %s=?, %s=?, %s=? where %s=? and %s=?";
        sql = String.format(sql,userTable,nameColumn,genderColumn,ageColumn,addressColumn,
                phoneColumn,emailColumn,schoolColumn,educationColumn,professionColumn,jobColumn,pcodeColumn,
                countColumn,pwdColumn);
        try {
            return jdbcTemplate.update(sql,name,gender,age,address,phone,email,school,education,
                    profession,job,pcode,account,pwd);
        }catch (IncorrectResultSizeDataAccessException e){
            return 0;
        }
    }

    public Object editCount(String newpwd,String email,String oldpwd){
        String sql = "update %s set %s=? where %s=? and %s = ?";
        sql = String.format(sql,userTable,pwdColumn,emailColumn,pwdColumn);
        try {
            return jdbcTemplate.update(sql,newpwd,email,oldpwd);
        }catch (IncorrectResultSizeDataAccessException e){
            return 0;
        }
    }


    @Override
    public String toString() {
        return "RegisterDao{" +
                "userTable='" + userTable + '\'' +
                ", countColumn='" + countColumn + '\'' +
                ", nameColumn='" + nameColumn + '\'' +
                ", pwdColumn='" + pwdColumn + '\'' +
                ", genderColumn='" + genderColumn + '\'' +
                ", ageColumn='" + ageColumn + '\'' +
                ", addressColumn='" + addressColumn + '\'' +
                ", phoneColumn='" + phoneColumn + '\'' +
                ", emailColumn='" + emailColumn + '\'' +
                ", schoolColumn='" + schoolColumn + '\'' +
                ", educationColumn='" + educationColumn + '\'' +
                ", professionColumn='" + professionColumn + '\'' +
                ", jobColumn='" + jobColumn + '\'' +
                ", pcodeColumn='" + pcodeColumn + '\'' +
                ", jdbcTemplate=" + jdbcTemplate +
                '}';
    }

    public String getPwdColumn1() {
        return pwdColumn1;
    }

    public void setPwdColumn1(String pwdColumn1) {
        this.pwdColumn1 = pwdColumn1;
    }

    public String getCountColumn() {
        return countColumn;
    }

    public void setCountColumn(String countColumn) {
        this.countColumn = countColumn;
    }

    public String getGenderColumn() {
        return genderColumn;
    }

    public void setGenderColumn(String genderColumn) {
        this.genderColumn = genderColumn;
    }

    public String getAgeColumn() {
        return ageColumn;
    }

    public void setAgeColumn(String ageColumn) {
        this.ageColumn = ageColumn;
    }

    public String getSchoolColumn() {
        return schoolColumn;
    }

    public void setSchoolColumn(String schoolColumn) {
        this.schoolColumn = schoolColumn;
    }

    public String getEducationColumn() {
        return educationColumn;
    }

    public void setEducationColumn(String educationColumn) {
        this.educationColumn = educationColumn;
    }

    public String getProfessionColumn() {
        return professionColumn;
    }

    public void setProfessionColumn(String professionColumn) {
        this.professionColumn = professionColumn;
    }

    public String getJobColumn() {
        return jobColumn;
    }

    public void setJobColumn(String jobColumn) {
        this.jobColumn = jobColumn;
    }

    public String getPcodeColumn() {
        return pcodeColumn;
    }

    public void setPcodeColumn(String pcodeColumn) {
        this.pcodeColumn = pcodeColumn;
    }

    public String getUserTable() {
        return userTable;
    }

    public void setUserTable(String userTable) {
        this.userTable = userTable;
    }

    public String getNameColumn() {
        return nameColumn;
    }

    public void setNameColumn(String nameColumn) {
        this.nameColumn = nameColumn;
    }

    public String getPwdColumn() {
        return pwdColumn;
    }

    public void setPwdColumn(String pwdColumn) {
        this.pwdColumn = pwdColumn;
    }

    public String getPhoneColumn() {
        return phoneColumn;
    }

    public void setPhoneColumn(String phoneColumn) {
        this.phoneColumn = phoneColumn;
    }

    public String getAddressColumn() {
        return addressColumn;
    }

    public void setAddressColumn(String addressColumn) {
        this.addressColumn = addressColumn;
    }

    public String getEmailColumn() {
        return emailColumn;
    }

    public void setEmailColumn(String emailColumn) {
        this.emailColumn = emailColumn;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
